package ddz;

import java.util.*;

public class capu {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int len = in.nextInt();
        int n = in.nextInt();
        in.nextLine();
        String[] str = in.nextLine().split(",");
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(str[i]);
        }

        // 新的数组，默认两边为地平面，即0,数组两端各加一个0
        int[] nums = new int[arr.length + 2];
        // 复制原数组的从第0位到length - 1，到新数组的第1位到length，长度为length,两端默认是0
        System.arraycopy(arr, 0, nums, 1, arr.length);

        // 单调栈存下标
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            //当栈顶为负数，且当前遍历的元素比栈顶元素要大，此时要弹栈
            while (!stack.isEmpty() && nums[stack.peek()] < 0 && nums[i] > nums[stack.peek()]) {
                int midIndex = stack.pop();
                int midHeight = nums[midIndex];
                if (stack.isEmpty())
                    break;
                int leftIndex = stack.peek(), leftHeight = nums[leftIndex];
                //  i - leftIndex - 1代表的是以midHeight为下界，可以装下的最大宽度
                // (Math.min(leftHeight, nums[i]) - midHeight)代表的是可以装几层，因为可能不只装一层
                res += (i - leftIndex - 1) / len * (Math.min(leftHeight, nums[i]) - midHeight);
            }
            stack.push(i);
        }
        System.out.println(res);
    }
}
/*
2
4
0,-1,-2,0

1
7
0,-1,-2,-1,1,-1,-1
 */